package com.lhkj.ct.meta.modules.gauge.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.mybatis.core.metadata.SqlOrder;
import com.lhkj.ct.base.mybatis.extension.service.impl.IServiceImpl;
import com.lhkj.ct.meta.modules.gauge.mapper.GaugeUsabilityMapper;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblGaugeUsability;
import com.lhkj.ct.meta.modules.gauge.service.GaugeUsabilityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 适用人群标签 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-10-08
 */
@Service
public class GaugeUsabilityServiceImpl extends IServiceImpl<GaugeUsabilityMapper, TblGaugeUsability> implements GaugeUsabilityService {

    @Override
    public IPage<TblGaugeUsability> selectUsabilityPage(TblGaugeUsability usability, PageQuery pageQuery) {
        LambdaUpdateWrapper<TblGaugeUsability> wrapper = new LambdaUpdateWrapper<TblGaugeUsability>()
                .like(StringUtils.isNotBlank(usability.getUName()), TblGaugeUsability::getUName, usability.getUName())
                .eq(Objects.nonNull(usability.getUType()), TblGaugeUsability::getUType, usability.getUType());
        return getBaseMapper().selectUsabilityPage(generatePageDesc(pageQuery, TblGaugeUsability::getCreateTime), wrapper);
    }

    @Override
    public IPage<Option<String>> selectUsabilityOptions(TblGaugeUsability usability, PageQuery pageQuery) {
        LambdaUpdateWrapper<TblGaugeUsability> wrapper = new LambdaUpdateWrapper<TblGaugeUsability>()
                .like(StringUtils.isNotBlank(pageQuery.getKeywords()), TblGaugeUsability::getUName, pageQuery.getKeywords())
                .eq(Objects.nonNull(usability.getUType()), TblGaugeUsability::getUType, usability.getUType());
        Page<TblGaugeUsability> page = generatePage(pageQuery, SqlOrder.desc(TblGaugeUsability::getCreateTime));
        return getBaseMapper().selectCategoryOptions(page,  wrapper);
    }

    @Override
    public boolean checkNameUnique(TblGaugeUsability usability) {
        LambdaUpdateWrapper<TblGaugeUsability> wrapper = new LambdaUpdateWrapper<TblGaugeUsability>()
                .like( TblGaugeUsability::getUName, usability.getUName())
                .eq( TblGaugeUsability::getUType, usability.getUType());
        wrapper.last("limit 1");
        TblGaugeUsability target = getBaseMapper().selectOne(wrapper);
        return null == target || StringUtils.equals(target.getId(), usability.getId());
    }
}
